package com.object;

import java.util.ArrayList;
import java.util.Scanner;

public class Order {
    private String id;
    private String state ;
    private double price=0;
    private String time;

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    private ArrayList<OrderList>orderLists = new ArrayList<>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    public ArrayList<OrderList> getOrderLists() {
        return orderLists;
    }

    public void setOrderLists(ArrayList<OrderList> orderLists) {
        this.orderLists = orderLists;
    }
    public User viewOrder(ArrayList<OperateLog>operateLogs,User user,Scanner scanner){
        for (Order order : user.getOrders()) {
            System.out.println("订单编号："+order.getId()+"  支付状态"+order.getState()+"   创建时间"+order.getTime()+"\n"+"总计："+order.getPrice());
        }
        return user;
    }
    public User payMyOrder(User user,Scanner scanner,ArrayList<Pet>pets){
        if (user.getOrders().isEmpty()){
            System.out.println("您当前没有订单");
            orderChoose(user,scanner,pets);
            return user;
        }
        System.out.println("请输入要支付的订单编号");
        String id = scanner.next();
        boolean isFind = false;
        for (Order order : user.getOrders()) {
            if(order.getId().equals(id)){
                isFind = true;
                //取到订单，再判断是否已经支付过了
                if(order.getState().equals("已支付")){
                    System.out.println("您已经付过钱了哦！！！，是否重新选？？？ 1:是   其他:否");
                    if(scanner.next().equals("1")){
                        payMyOrder(user,scanner,pets);
                    }else {
                        break;
                    }
                    break;
                }else{
                    //否则付钱
                    System.out.println("总价:"+order.getPrice());
                    System.out.println("已支付");
                    order.setState("已支付");
                    //付钱的同时，将狗的库存移除
                    new Pet().removePet(user,pets);
                }
            }
        }
        if(!isFind){
            System.out.println("未查询到此id");
            payMyOrder(user,scanner,pets);
        }
        return user;
    }
    public User orderChoose(User user,Scanner scanner,ArrayList<Pet>pets){
        System.out.println("请选择1:查看订单详情   2:删除订单   3:支付订单    4:取消订单    其他:返回");
        switch (scanner.next()){
            case "1":
                viewOrderMessage(user,scanner,pets);
                break;
            case  "2":
                removeOrder(user,scanner,pets);
                break;
            case  "3":
                payMyOrder(user,scanner,pets);
                break;
            case  "4":
                cancelOrder(user,scanner,pets);
                break;
            default:
        }
        return  user;
    }
    //取消订单
    public User cancelOrder(User user,Scanner scanner,ArrayList<Pet>pets){
        if (user.getOrders().isEmpty()){
            System.out.println("您当前没有订单");
            orderChoose(user,scanner,pets);
            return user;
        }
        System.out.println("请选择您要取消的订单编号");
        if (user.getOrders().isEmpty()){
            return user;
        }
        String id = scanner.next();//订单编号
        boolean isFind = false;
        for (Order order : user.getOrders()) {
            if(order.getId().equals(id)){
                isFind = true;
                if(order.getState().equals("待支付")){
                    order.setState("已取消");
                }
            }
        }
        if(!isFind){
            System.out.println("商品id不存在");
            cancelOrder(user,scanner,pets);
        }
        return user;
    }
   public void viewOrderMessage(User user,Scanner scanner,ArrayList<Pet>pets){
        //选择订单打印详情
       if (user.getOrders().isEmpty()){
           orderChoose(user,scanner,pets);
           return;
       }
       System.out.println("请输入想要查看的订单编号详情");

       String id = scanner.next();//订单编号
       for (Order order : user.getOrders()) {
           if (order.getId().equals(id)){
               System.out.println("订单编号"+order.getId());
               for (OrderList orderList : order.getOrderLists()) {
                   System.out.println("宠物ID"+orderList.getId()+"    宠物昵称:"+orderList.getName()+"    宠物性别:"+orderList.getGender()+"    宠物年龄"+orderList.getAge()+"    宠物种类:"+orderList.getKind()+"    宠物毛色:"+orderList.getColor()+"    宠物价格:"+orderList.getPrice());
               }
               System.out.println("订单生成时间"+order.getTime()+"总价"+order.getPrice());
           }
       }
   }
   //
    public void removeOrder(User user,Scanner scanner,ArrayList<Pet>pets){
        if (user.getOrders().isEmpty()){
            System.out.println("您当前没有订单");
            orderChoose(user,scanner,pets);
            return ;
        }
        System.out.println("请选择要删除的订单编号");
        String id = scanner.next();//订单编号
        for (Order order : user.getOrders()) {
            if(order.getId().equals(id)){
                if(order.getState().equals("已支付")){
                    user.getOrders().remove(order);
                    System.out.println("已删除");
                    System.out.println("是否继续删除？ 1:继续   其他:大可不必");
                    if(scanner.next().equals("1")){
                        removeOrder(user,scanner,pets);
                    }
                    break;
                }else {
                    System.out.println("无法删除，可以取消订单");
                }
            }
        }
    }
}
